package com.hkts.naturalgas.summary.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.dictionaries.utils.DBUtils;
import com.hkts.naturalgas.summary.DateEnum;
import com.hkts.naturalgas.summary.entity.SummaryDictionary;
import com.hkts.naturalgas.summary.mapper.SummaryDictionaryMapper;
import com.hkts.naturalgas.summary.service.ISummaryDictionaryService;
import com.hkts.naturalgas.summary.vo.SummaryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2022-02-07
 */
@Service
@Transactional
@Slf4j
public class SummaryDictionaryServiceImpl extends ServiceImpl<SummaryDictionaryMapper, SummaryDictionary> implements ISummaryDictionaryService {


    @Autowired
    private SummaryDictionaryMapper summaryDictionaryMapper;
    @Autowired
    private DBUtils dBUtils;

    @Override
    public IPage<SummaryDictionary> selectPage(Page<SummaryDictionary> page, SummaryDictionary summaryDictionary) {
        QueryWrapper<SummaryDictionary> query = Wrappers.query();

        return summaryDictionaryMapper.selectPage(page, query);
    }
    @Override
    public List<SummaryDictionary> selectList(SummaryDictionary summaryDictionary) {
        QueryWrapper<SummaryDictionary> query = Wrappers.query();
        return summaryDictionaryMapper.selectList(query);
    }
    @Override
    public SummaryVo getListByTableIdAndDate(String tableId, String date) {
        SummaryVo vo=new SummaryVo();
        QueryWrapper<SummaryDictionary> query = Wrappers.query();
        query.lambda().eq(SummaryDictionary::getTableId, tableId);
        SummaryDictionary summaryDictionary = summaryDictionaryMapper.selectOne(query);
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
        String format = sim.format(new Date());
        List<SummaryVo> list=new ArrayList<>();
        //今日小时汇总数据
        List<Map<String, Object>> summaryByTableAndDate1 = dBUtils.getSummaryByTableAndDate(summaryDictionary.getSummaryTable(), format);
        //历史小时汇总数据
        List<Map<String, Object>> summaryByTableAndDate = dBUtils.getSummaryByTableAndDate(summaryDictionary.getSummaryTable(), date);
        List<Map<String, Object>> maps = getsummaryList(summaryByTableAndDate1,"今日");
        maps.get(0).put("key","todaySo2");
        maps.get(1).put("key","todayNox");
        maps.get(2).put("key","todayWasteGas");
        maps.get(3).put("key","todaySmoke");
        List<Map<String, Object>> maps1 = getsummaryList(summaryByTableAndDate, date);
        maps1.get(0).put("key","historySo2");
        maps1.get(1).put("key","historyNox");
        maps1.get(2).put("key","historyWasteGas");
        maps1.get(3).put("key","historySmoke");
        maps.addAll(maps1);
        vo.setList(maps);
        //今日汇总数据
        List<Map<String, Object>> daySummaryByTableAndDate = dBUtils.getDaySummaryByTableAndDate(summaryDictionary.getSummaryTable(), format);
        if(daySummaryByTableAndDate.size()>0){
            vo.setTodaySo2(Float.parseFloat(daySummaryByTableAndDate.get(0).get("so2").toString()));
            vo.setTodayNox(Float.parseFloat(daySummaryByTableAndDate.get(0).get("nox").toString()));
            vo.setTodayWasteGas(Float.parseFloat(daySummaryByTableAndDate.get(0).get("wasteGas").toString()));
            vo.setTodaySmoke(Float.parseFloat(daySummaryByTableAndDate.get(0).get("smoke").toString()));
        }
        //历史汇总数据
        List<Map<String, Object>> daySummaryByTableAndDatehis = dBUtils.getDaySummaryByTableAndDate(summaryDictionary.getSummaryTable(), date);
        if(daySummaryByTableAndDatehis.get(0).get("so2")!=null){
            vo.setHistorySo2(Float.parseFloat(daySummaryByTableAndDatehis.get(0).get("so2").toString()));
        }
        if(daySummaryByTableAndDatehis.get(0).get("nox")!=null){
            vo.setHistoryNox(Float.parseFloat(daySummaryByTableAndDatehis.get(0).get("nox").toString()));
        }
        if(daySummaryByTableAndDatehis.get(0).get("wasteGas")!=null){
            vo.setHistoryWasteGas(Float.parseFloat(daySummaryByTableAndDatehis.get(0).get("wasteGas").toString()));
        }
        if(daySummaryByTableAndDatehis.get(0).get("smoke")!=null){
            vo.setHistorySmoke(Float.parseFloat(daySummaryByTableAndDatehis.get(0).get("smoke").toString()));
        }
        return vo;
    }
    public List<Map<String, Object>> getsummaryList(List<Map<String, Object>> summaryByTableAndDate,String date){
        List<Map<String, Object>> listResult=new ArrayList<>();
        Map<String, Object> S02Map = getMap();
        S02Map.put("type",date+"二氧化硫排放量/kg");
        Map<String, Object> N02Map = getMap();
        N02Map.put("type",date+"氮氧化物排放量/kg");
        Map<String, Object> wastGasMap = getMap();
        wastGasMap.put("type",date+"废气排放量/m3");
        Map<String, Object> smokeMap = getMap();
        smokeMap.put("type",date+"烟尘排放量/kg");
        for(int i=0;i<summaryByTableAndDate.size();i++){
            int time=Integer.parseInt(summaryByTableAndDate.get(i).get("time").toString());
            S02Map.put(DateEnum.getTimeFiled(time),summaryByTableAndDate.get(i).get("so2"));
            N02Map.put(DateEnum.getTimeFiled(time),summaryByTableAndDate.get(i).get("nox"));
            wastGasMap.put(DateEnum.getTimeFiled(time),summaryByTableAndDate.get(i).get("wasteGas"));
            smokeMap.put(DateEnum.getTimeFiled(time),summaryByTableAndDate.get(i).get("smoke"));
        }
        listResult.add(S02Map);
        listResult.add(N02Map);
        listResult.add(wastGasMap);
        listResult.add(smokeMap);
        return listResult;
    }
    public Map<String,Object> getMap(){
     Map<String,Object> map=new HashMap<String,Object>();
     for(int i=0;i<24;i++){
         map.put(DateEnum.getTimeFiled(i),null);
     }
     return map;
    }
}
